﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartParking.Entities;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System.Globalization;

namespace SmartParking.ORM
{
    public class SmartParkingDbContext : DbContext
    {
        public SmartParkingDbContext(DbContextOptions<SmartParkingDbContext> options)
            : base(options)
        { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
                optionsBuilder.UseSqlServer("Data Source=WIN-Q89MVRJ46UD;Initial Catalog=SP2004;User ID=sa;Password=123456;Encrypt=False;Trust Server Certificate=False");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //联合主键
            modelBuilder.Entity<RoleMenu>().HasKey(pk => new { pk.RoleId, pk.MenuId });
            modelBuilder.Entity<RoleUser>().HasKey(pk => new { pk.RoleId, pk.UserId });
            //角色表一对多关系
            modelBuilder.Entity<RoleUser>()
                .HasOne(u => u.SysRole)
                .WithMany(r => r.Users)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<RoleMenu>()
                .HasOne(u => u.SysRole)
                .WithMany(r => r.Menus)
                .OnDelete(DeleteBehavior.Cascade);
            //用户-角色
            modelBuilder.Entity<RoleUser>().HasOne(u => u.User).WithMany(r => r.Roles);

            // 页面-》数据库的转换
            // 数据库-》页面的转换
            ValueConverter iconValueConverter =
                new ValueConverter<string, string>(
                    p2d => string.IsNullOrEmpty(p2d) ? null : ((int)p2d.ToArray()[0]).ToString("x"),
                    d2p => d2p == null ? "" : ((char)int.Parse(d2p, NumberStyles.HexNumber)).ToString()
                    );
            modelBuilder.Entity<SysMenu>()
                .Property(m => m.MenuIcon)
                .HasConversion(iconValueConverter);
        }
        public virtual DbSet<SysUser> SysUser { get; set; }

        public virtual DbSet<SysMenu> SysMenu { get; set; }

        public virtual DbSet<SysRole> SysRole { get; set; }
        public virtual DbSet<RoleUser>  RoleUser { get; set; }
        public virtual DbSet<RoleMenu> RoleMenu { get; set; }

        public virtual DbSet<DeviceInfo> DeviceInfo { get; set; }

        public virtual DbSet<AutoRegister> AutoRegister { get; set; }
        public virtual DbSet<BaseLicenseColor> BaseLicenseColor { get; set; }
        public virtual DbSet<BaseLicenseType> BaseLicenseType { get; set; }
        public virtual DbSet<BaseAutoColor> BaseAutoColor { get; set; }
        public virtual DbSet<BaseFeeMode> BaseFeeMode { get; set; }
        public virtual DbSet<MemberRecharge> MemberRecharge { get; set; }
        public virtual DbSet<RecordInfo> RecordInfo { get; set; }
        public virtual DbSet<OrderInfo> OrderInfo { get; set; }


    }
}
